<?php

include_once('_path.php');

include_once(_CONST_LOCAL_INCLUDE_PATH."database.class.php");
$database = new Database($db_vars);

include_once(_CONST_LOCAL_INCLUDE_PATH."common.php");

#论坛信息
include_once(_CONST_LOCAL_INCLUDE_PATH."session.include.php");

$quote = array();

$action = trim($_POST['action']);
$idx = intval($_POST['idx']);
$series_id = intval($_POST['series_id']);
  $quote['series_id'] = string_sql($series_id);
$chapter_id = intval($_POST['chapter_id']);
  $quote['chapter_id'] = string_sql($chapter_id);
$page = intval($_POST['page']);
if ($page < 1) $page = 1;
  $quote['page'] = string_sql($page);
$user_id = intval($user['userid']);
  $quote['user_id'] = string_sql($user_id);

echo "<script>";

if (!$user_id) {
  ?>
	alert("You need to login first!");
  $('login').setStyle('display','block');
	<?
} else if ($action == 'add' && ($series_id > 0 || $chapter_id > 0)) {
	$quote['entry_time'] = string_sql(time());

	$database->execute("REPLACE INTO bookmark (user_id, series_id, chapter_id, page, entry_time, last_access) VALUES (".$quote['user_id'].", ".$quote['series_id'].", ".$quote['chapter_id'].", ".$quote['page'].", ".$quote['entry_time'].", ".$quote['entry_time'].")");

	?>
	alert("Bookmarked");
	<?
} else if ($action == 'del' && $idx > 0 && ($series_id > 0 || $chapter_id > 0)) {
	if ($chapter_id > 0) {
		$database->execute("DELETE FROM bookmark WHERE series_id = ".$quote['series_id']." AND chapter_id = ".$quote['chapter_id']." AND user_id = ".$quote['user_id']);
	} else {
		$database->execute("DELETE FROM bookmark WHERE series_id = ".$quote['series_id']." AND user_id = ".$quote['user_id']);
	}
	
	?>
  $('bk<?= $idx ?>').dispose();
	alert("Removed");
	<?
}

echo "</script>";

?>